package com.hmall.user.filters;

import com.hmall.utils.UserThreadLocal;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * TODO
 *
 * @Author lyh
 * @Date 2024/11/15 8:51
 */
@Component
public class UserHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断当前请求是否是handler()
        if(!(handler instanceof HandlerMethod)){
            return true;
        }
        String authorization = request.getHeader("authorization");
        if(authorization!=null) {
            UserThreadLocal.setSubject(authorization);
            return true;
        }
        return false;
    }
}
